﻿#include "640search.h"

_640Search::_640Search(Map *map0):currentPoint(-1,-1)
{
    setMap(map0);
    distance=0;
    stepCount=0;
}
void _640Search::setMap(Map *map0)
{
    map=map0;
    currentPoint=map->getStart();
}

void _640Search::setDistance(int distance0)
{
    distance=distance0;
}

void DFS::step()
{
    int c;
    int a=0;
    Point pre=map->getStart();
    if(pre==currentPoint) s.push(currentPoint);
    for(int i=0;i<8;i++)
    {
        currentPoint.x+=DFSdx[i];
        currentPoint.y+=DFSdy[i];
        if(map->is(currentPoint)!=CLEAR)
        {
            currentPoint.x-=DFSdx[i];
            currentPoint.y-=DFSdy[i];
        }
        else
        {
            c=DFStest(currentPoint,i);
            if(c==0)
            {
                currentPoint.x-=DFSdx[i];
                currentPoint.y-=DFSdy[i];
            }
            else
            {
                //changestate(currentPoint);  //改格子state为PATH
                map->changeState(currentPoint,PATH);
                break;
            }
        }
        a++;     //
        if(a==7)
        {
            map->clearOutputData(currentPoint);
            changestate(currentPoint); //改变该格子的状态，改为不可走
            Point p=s.pop();
            currentPoint=s.top();  //获得栈顶
        }

    }
    emit completed(true);
    map->output(currentPoint,pre,PATH,"Step:"+QString::number(++stepCount));
//    qDebug()<<"("<<currentPoint.x<<","<<currentPoint.y<<")";
    if(currentPoint==map->getEnd()){
        isFinished=1;
        emit finished(true);
    }
    if(currentPoint==map->getStart()){
        emit finished(false);
    }
}

void _640Search::DFS_Continuous()
{
//    do
//    {
//        DFS_step();
//    }while(emit finished(true))
}

//void _640Search::stop()
//{
//    ;
//}

void BFS::step()
{
    int c;
//    currentNode.p=currentPoint;
    Node pre;
    pre.p=map->getStart();
    if(pre.p==currentPoint) q.enqueue(currentNode); currentNode.next=NULL;//
    if(!q.isEmpty())
    {
        currentNode=q.dequeue();
        currentPoint=currentNode.p;
        pre=currentNode;
        for(int i=0;i<8;i++)
            {
                pre.p.x+=BFSdx[i];
                pre.p.y+=BFSdy[i];
                if(map->is(pre.p)!=CLEAR)
                {
                    pre.p.x-=BFSdx[i];
                    pre.p.y-=BFSdy[i];
                }
                else
                {
                    c=BFStest(pre,i);
                    if(c==0)
                    {
                        pre.p.x-=BFSdx[i];
                        pre.p.y-=BFSdy[i];
                    }
                    else
                    {
                        q.enqueue(pre);            //将周围的点全部遍历
                        changestate(pre.p);          //改变当前点的state
                        pre.next=&currentNode;
                    }
                }
            }
    }

    emit completed(true);
    map->output(currentPoint,pre.p,PATH,"Step:"+QString::number(++stepCount));
//    qDebug()<<"("<<currentPoint.x<<","<<currentPoint.y<<")";
    if(currentPoint==map->getEnd()){
        isFinished=1;
        emit finished(true);
    }
    if(currentPoint==map->getStart()){
        emit finished(false);
    }
}


void _640Search::BFS_Continuous()
{
//    do
//    {
//        BFS_step();
//    }while(emit finished(true))
}
void DFS::Continuous()
{
    while(!isFinished){
    step();
    }
}

void DFS::stop()
{
    ;
}
void BFS::Continuous()
{
    while(!isFinished){
    step();
    }
}

void BFS::stop()
{
    ;
}
